﻿var app = angular.module('importer', []);
var nrOfRows = 0;

app.controller('appController', ['$window', '$scope', function ($window, $scope) {
    this.scriptRows = $window.scriptRows;
    nrOfRows = $window.nrOfRows;
    $scope.updateScriptRows = function () {
        this.scriptRows = $window.scriptRows;
        nrOfRows = $window.nrOfRows;
    };

    $scope.setTooltip = function () {
        $('.grayDiv.redDiv').tooltip({
            animated: 'fade',
            placement: 'top',
        });
    };

}]);

app.directive('sendRowToServer', function () {
    return {
        restrict: 'A',
        templateUrl: '/Content/js/Views/Import/send-row-to-server.html',
        controller: ['$http', '$window', '$scope', function ($http, $window, $scope) {
            this.show = 0;            
            var directive = this;
            this.setUpData = function (row) {
                this.data = row;

                jQuery.ajax({
                    url: "/api/processrow/",
                    type: "POST",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", getAuthorizationHeader($window.UserId, ''));
                    },
                    dataType: "json",
                    data: { '': JSON.stringify(directive.data) },
                    success: function (mydata) {                       
                        console.log(mydata);
                        directive.response = mydata;
                        var pVal = $('#progressbar').progressbar('option', 'value');
                        var deleteVal = 2;
                        if (nrOfRows < 3)
                            deleteVal = 1;
                        $("#progressbar").progressbar({
                            value: pVal + (100 / (nrOfRows - 2))
                        });

                        angular.element($('#appControllerDiv')).scope().$apply();
                    },
                    error: function () {
                        console.log("AJAX ERROR");
                    },
                    complete: function () {
                        angular.element($('#appControllerDiv')).scope().setTooltip();
                    }
                });
            };



            var _this = this;
            this.rowClicked = function () {
                if (this.show != 1) {
                    this.show = 1;
                } else {
                    this.show = 0;
                }
            };

            this.addLoanable = function (data) {
                jQuery.ajax({
                    url: "/api/import/",
                    type: "POST", beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", getAuthorizationHeader($window.UserId, ''));
                    },
                    dataType: "json",
                    data: { '': JSON.stringify(data) },
                    success: function (mydata) {
                        directive.response = mydata;
                        _this.rowClicked();
                        angular.element($('#appControllerDiv')).scope().$apply();
                    },
                    error: function () {
                        console.log("AJAX ERROR");
                    },
                    complete: function () {
                        angular.element($('#appControllerDiv')).scope().setTooltip();
                    }
                });
            }


            this.tryAgain = function (data) {
                var dataArr = [];
                dataArr.push(data);
                jQuery.ajax({
                    url: "/api/processrow/",
                    type: "POST",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", getAuthorizationHeader($window.UserId, ''));
                    },
                    dataType: "json",
                    data: { '': JSON.stringify(dataArr) },
                    success: function (mydata) {
                        console.log(mydata);
                        directive.response = mydata;
                        _this.rowClicked();
                        angular.element($('#appControllerDiv')).scope().$apply();
                    },
                    error: function () {
                        console.log("AJAX ERROR");
                    },
                    complete: function () {
                        angular.element($('#appControllerDiv')).scope().setTooltip();
                    }
                });
            }

        }

        ],
        controllerAs: 'rowCtrl'

    };
});